package wstrader.task.warehouse.weighing;

import pc.system.*;
import pc.util.*;
import wstrader.task.*;

public class WN extends Role
{
  public WNR wnr = null;
  public long id = 0;
  private char type;
  private String stype = null;
  private String wn_table = null;
  private String inst_table = null;
  //private String order_table = null;
  private boolean updated = false;

  public WN(Tasks owner, char type)
  {
    super(owner);//,name,menu);
    wnr = new WNR(this);
    this.type = type;
    switch (type) {
        case WN_IMPORT :
          stype = "IM";
          inst_table = "delivery_instruction";
          break;
        case WN_IN_PRO :
          stype = "IP";
          inst_table = "processing_instruction";
          //inst_table  = order_table;
          break;
        case WN_EX_PRO :
          stype = "XP";
          inst_table = "processing_instruction";
          //inst_table  = order_table;
          break;
        case WN_EXPORT :
          stype = "EO";
          inst_table = "shipping_instruction";
          break;
    }
    wn_table = "WEIGHT_NOTE_" + stype;
  }

  public boolean isUpdated()
  {
    return updated;
  }

  public char getType()
  {
    return this.type;
  }

  public String getInst_Table()
  {
    return inst_table;
  }

  public String getWN_Table()
  {
    return wn_table;
  }

  public String getNew_WN()
  {
    String sql =
        "SELECT MAX(ref_number) as ref_number " +
        "FROM " + wn_table + " ";
        //"WHERE inst_id = " + inst_id;
    Record rc = new Record(this.getSQLCommand().executeQuery(sql));
    String ref = (!rc.isEmpty()) ? rc.getString("ref_number") : stype + " 00-00000";
    rc.close();
    return "WN-" + stype + " " + this.getNextRefNumber(ref);
  }

  private long save_QR(String wn_ref_number)
  {
    String qr_ref_number = wn_ref_number.replaceFirst("WN-", "QR-");
    Table tab = new Table(this, "quality_report");
    tab.addParam("ref_number", qr_ref_number);
    tab.addParam("type", "" + type);
    return tab.update("ref_number", qr_ref_number);
  }

  public long save_WN()
  {
    //long wn_id = getRequest().getLongValue("wn_id");
    boolean new_wn = (id < 0);
    id = this.doUpdate(wn_table, id);
    //
    Table wn_tab = new Table(this, wn_table);
    if (new_wn) {
      String wn_ref_number = getNew_WN();
      long qr_id = save_QR(wn_ref_number);
      wn_tab.addParam("ref_number", wn_ref_number);
      wn_tab.addParam("qr_id", "" + qr_id);
   } else {
      String wn_ref_number = new Table(this,wn_table).getRecord(id).getStringValue("ref_number");
      long qr_id = save_QR(wn_ref_number);
      wn_tab.addParam("qr_id", "" + qr_id);
    }

    wn_tab.addParam("date", Utils.getCurDate());
    wn_tab.addParam("time", Utils.getCurTime());
    wn_tab.addParam("user_id", "" + getUser().getUserID());
    id = wn_tab.update(id);
    return id;
  }

  private boolean delete_WN()
  {
    String sql = "DELETE FROM " + wn_table + " WHERE id = " + id;
    return getSQLCommand().executeUpdate(sql) > 0;
  }

  public long doTask()
  {
    id = getRequest().getLongValue("wn_id");
    int task_id = getRequest().getIntValue("task_id");
    switch (task_id) {
      case 1:
        id = save_WN();
        wnr.save_WNR();
        wnr.sum_WNR2WN();
        updated = true;
        break;
      case 2 :
        if (id < 0)  id = save_WN();
        wnr.save_WNR(getRequest().getIntValue("no"));
        wnr.sum_WNR2WN();
        updated = true;
        break;
      case 3 : // delete WN
        wnr.delete_WNR();
        if (delete_WN()) id = 0;
        updated = true;
        break;
      case 4 :
        wnr.delete_WNR(getRequest().getIntValue("no"));
        wnr.sum_WNR2WN();
        updated = true;
        break;
    }
    return id;
  }
}
